package com.windoent.yugioh.ui.activity.meinfo;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.baidu.mobstat.StatService;
import com.windoent.yugioh.MainActivity;
import com.windoent.yugioh.R;
import com.windoent.yugioh.base.BaseActivity;
import com.windoent.yugioh.http.PhoneLive;
import com.windoent.yugioh.utils.BaseMethod;
import com.windoent.yugioh.utils.BaseUtils;
import com.windoent.yugioh.utils.ToastUtils;
import com.windoent.yugioh.widget.ActivityTitle;
import com.windoent.yugioh.widget.dialog.SucessFailAutoDismissDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;

import okhttp3.Call;

/**
 * author : robert
 * date   : 2021/5/10 10:27
 * desc   :店家实名认证
 */
public class StoreAuthRealNameActivity extends BaseActivity {
    private ActivityTitle mActivityTitle;
    private EditText mEtAuthName;
    private EditText mEtIdNumber;
    private Button mBtnGetCode;

    @Override
    public int getLayout() {
        return R.layout.activity_store_auth_real_name;
    }

    @Override
    public void initView() {
        super.initView();
        mActivityTitle = (ActivityTitle) findViewById(R.id.activity_title);
        immersiveStatusBar(mActivityTitle.getView());
        mEtAuthName = (EditText) findViewById(R.id.et_auth_name);
        mEtIdNumber = (EditText) findViewById(R.id.et_id_number);
        mBtnGetCode = (Button) findViewById(R.id.btn_get_code);

    }

    @Override
    public void initListener() {
        super.initListener();
        mActivityTitle.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mBtnGetCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取验证码
                String authName = mEtAuthName.getText().toString().trim();
                String authNumber = mEtIdNumber.getText().toString().trim();
                if (BaseMethod.isNull(authName)) {
                    ToastUtils.showText(getString(R.string.string_input_real_name_null));
                } else if (BaseMethod.isNull(authNumber)) {
                    ToastUtils.showText(getString(R.string.string_input_id_number_null));
                } else {
                    dianjiaAuthRealName(authName, authNumber);
                }
            }
        });
    }

    //验证真实身份
    private void dianjiaAuthRealName(String authName, String authNumber) {
        showLoadingDialog("");
        PhoneLive.realNameAuth(getUserToken(), authNumber, authName, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                hideLoadingDialog();
                ToastUtils.showText(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                hideLoadingDialog();
                if (BaseUtils.isJSONValid(response)) {
                    try {
                        if (BaseUtils.codeIs0(mContext, response)) {
                            dianjiaAuthRealNameDialog(true);
                        } else dianjiaAuthRealNameDialog(false);
                    } catch (JSONException e) {
                        dianjiaAuthRealNameDialog(false);
                        e.printStackTrace();
                    }
                } else {
                    dianjiaAuthRealNameDialog(false);
                    ToastUtils.showText(getString(R.string.string_response_error));
                }

            }
        });
    }

    //验证真实身份对话框
    private void dianjiaAuthRealNameDialog(boolean sucess) {
        SucessFailAutoDismissDialog dialog = new SucessFailAutoDismissDialog(StoreAuthRealNameActivity.this, new SucessFailAutoDismissDialog.OnInterface() {
            @Override
            public void onDissmiss() {
                if (sucess) {
                    startActivity(new Intent(StoreAuthRealNameActivity.this, StoreAuthActivity.class));
                    finish();
                }
            }
        });
        if (sucess)
            dialog.setTvContent(true, R.drawable.drawable_dialog_sucess, getString(R.string.string_auth_real_name_sucess), "", "");
        else
            dialog.setTvContent(false, R.drawable.drawable_dialog_fail, getString(R.string.string_auth_real_name_fail), getString(R.string.string_check_input_more), "");
        dialog.show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        OkHttpUtils.getInstance().cancelTag(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onPageStart(this, "店家实名认证界面");
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPageEnd(this, "店家实名认证界面");
    }
}
